<template>
	<el-config-provider :size="getGlobalComponentSize" :locale="zhCn">
		<router-view v-if="isRouterActive" v-show="setLockScreen" />
		<LockScreen v-if="themeConfig.isLockScreen" />
		<Setings ref="setingsRef" v-show="setLockScreen" />
		<CloseFull v-if="!themeConfig.isLockScreen" />
		<Upgrade v-if="getVersion" />
		<Sponsors v-if="false" />
	</el-config-provider>
</template>

<script setup name="app">
 import opentype from 'opentype.js'
	import {provide} from 'vue'
	import {defineAsyncComponent, computed, ref, onBeforeMount, onMounted, onUnmounted, nextTick, watch} from 'vue'
	import {useRoute,useRouter} from 'vue-router'
	import zhCn from 'element-plus/es/locale/lang/zh-cn'
	import {storeToRefs} from 'pinia'
	import {useTagsViewRoutes} from '/@/stores/tagsViewRoutes'
	import {useFontsStore} from '/@/stores/fonts'
	import {useThemeConfig} from '/@/stores/themeConfig'
	import other from '/@/utils/other'
	import {Local, Session} from '/@/utils/storage'
	import setIntroduction from '/@/utils/setIconfont'
	import mittBus from './utils/mitt'
	import JSZipUtils from "jszip-utils";
import docxtemplater from "docxtemplater";
import { saveAs } from "file-saver";
import JSZip from "pizzip";
import ImageModule from "docxtemplater-image-module-free";
import '/@/utils/checkJSChange'
	import axios from 'axios'

	// 引入组件
//   let a 
	let router = useRouter()
	const LockScreen = defineAsyncComponent(() => import('/@/layout/lockScreen/index.vue'))
	const Setings = defineAsyncComponent(() => import('/@/layout/navBars/topBar/setings.vue'))
	const CloseFull = defineAsyncComponent(() => import('/@/layout/navBars/topBar/closeFull.vue'))
	const Upgrade = defineAsyncComponent(() => import('/@/layout/upgrade/index.vue'))
	const Sponsors = defineAsyncComponent(() => import('/@/layout/sponsors/index.vue'))

	// 定义变量内容
	const setingsRef = ref()
	const route = useRoute()
	const stores = useTagsViewRoutes()
	const storesThemeConfig = useThemeConfig()
	const {themeConfig} = storeToRefs(storesThemeConfig)
   const {fonts} = storeToRefs(useFontsStore())
	// 设置锁屏时组件显示隐藏
	const setLockScreen = computed(() => {
		// 防止锁屏后，刷新出现不相关界面
		// https://gitee.com/lyt-top/vue-next-admin/issues/I6AF8P
		return themeConfig.value.isLockScreen ? themeConfig.value.lockScreenTime > 1 : themeConfig.value.lockScreenTime >= 0
	})

	const isRouterActive = ref(true)

	provide('reload', () => {
		isRouterActive.value = false
		nextTick(() => {
			isRouterActive.value = true
		})
	})
	let reFrush = () => {
  sessionStorage.setItem('path',route.fullPath)
  router.push({name:'noPower'})
}
	provide('reFrush',reFrush)


	// 获取版本号
	const getVersion = computed(() => {
		let isVersion = false
		if (route.path !== '/login') {
			if ((Local.get('version') && Local.get('version') !== __NEXT_VERSION__) || !Local.get('version')) isVersion = true
		}
		return isVersion
	})
	// 获取全局组件大小
	const getGlobalComponentSize = computed(() => {
		return other.globalComponentSize()
	})

	// 设置初始化，防止刷新时恢复默认
	onBeforeMount(() => {

		  opentype.load('./SourceHanSans-VF.ttf', async function (err, font) {
			fonts.value = font

	  
    });
		// 设置批量第三方 icon 图标
		setIntroduction.cssCdn()
		// 设置批量第三方 js
		setIntroduction.jsCdn()


		  
	})

	
	// 页面加载时
	onMounted(() => {
		window.addEventListener('click',(e)=>{
			
			[...document.querySelectorAll('.layout-navbars-tagsview-ul-li')].some(v=>{
           return v.contains(e.target)
			})
		
			
		})
		nextTick(() => {
			// 监听布局配置弹窗点击打开
			mittBus.on('openSetingsDrawer', () => {
				setingsRef.value.openDrawer()
			})
			// 获取缓存中的布局配置
			if (Local.get('themeConfig')) {
				storesThemeConfig.setThemeConfig({themeConfig: Local.get('themeConfig')})
				document.documentElement.style.cssText = Local.get('themeConfigStyle')
			}
			// 获取缓存中的全屏配置
			if (Session.get('isTagsViewCurrenFull')) {
				stores.setCurrenFullscreen(Session.get('isTagsViewCurrenFull'))
			}
		})
	})
	// 页面销毁时，关闭监听布局配置/i18n监听
	onUnmounted(() => {
		mittBus.off('openSetingsDrawer', () => {})
	})
	// 监听路由的变化，设置网站标题
	watch(
		() => route.path,
		() => {
			other.useTitle()
		},
		{
			deep: true,
		}
	)




// 	let changePageSize = () => {
//     document.body.style.zoom = 1/window.devicePixelRatio
//  }
//  changePageSize()

//  setInterval(()=>{
// 	changePageSize()
//  },1000)


 // 监听消息
 
//  window.addEventListener('message', dealMessage);
//   // 监听消息的回调函数
//  function dealMessage(e) {
//     console.log(e,'1232312hhh')

// 	setTimeout(()=>{
// 		window.top.postMessage('hello, parent!', '*');
// 	},5000)
//   }


// let exportWordDocx1 = (obj) => {
//  const url = "/test2.docx";
//   JSZipUtils.getBinaryContent(url, (err, data) => {
//     if (err) {
     
//       return;
//     }
//     let zip = new JSZip(data);
//     const Docxtemplater = docxtemplater;
//     const imageOptions = {
//       centered: false,
//       getImage(tagValue, tagName) {
//         return new Promise(function (resolve, reject) {
//           JSZipUtils.getBinaryContent(tagValue, function (error, content) {
//             if (error) {
//               return reject(error);
//             }
//             return resolve(content);
//           });
//         });
//       },
//       getSize(img, tagValue, tagName) {
//         // 非固定大小（这里是设置宽度最大为500px的图片）
//         return new Promise(function (resolve, reject) {
//           let image = new Image();
//           image.src = tagValue;
//           let imgWidth, imgHeight, scale;
//           image.onload = function () {
//             imgWidth = image.width;
//             imgHeight = image.height;
//             scale = 0;
//             if (imgWidth > 500) {
//               scale = 500 / imgWidth;
//               imgWidth = 500;
//               imgHeight = imgHeight * scale;
//             }
//             resolve([imgWidth, imgHeight]);
//           };
//           image.onerror = function (e) {
           
//             reject(e);
//           };
//         });
//       },
//     };

   

//     let doc = new Docxtemplater().loadZip(zip).setOptions({
//             // delimiters: { start: "[[", end: "]]" },
//             paragraphLoop: true,
//             linebreaks: true,
//         }).attachModule(new ImageModule(imageOptions)).compile()


//     let reportInfo = {};
//     obj.training_content_arr = obj.training_content.split("/n");
//     obj.create_time = obj.create_time.substring(
//       0,
//       obj.create_time.indexOf(" ")
//     );
//     reportInfo = { ...obj };
//     doc.resolveData({ ...reportInfo }).then(() => {
//       doc.render();
//       let out = doc.getZip().generate({
//         type: "blob",
//         MimeType:
//           "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
//       })

//       saveAs(out, "内部培训记录表.docx");
//     });
//   });



// };


</script>
